package com.luoqy.speedy.modular.web.service.impl;


import java.util.concurrent.Future;

import javax.annotation.Resource;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.luoqy.speedy.modular.web.aop.HttpAspect;
import com.luoqy.speedy.modular.web.mapper.ChapterContentMapper;
import com.luoqy.speedy.modular.web.mapper.ChapterMapper;
import com.luoqy.speedy.modular.web.model.Chapter;
import com.luoqy.speedy.modular.web.model.ChapterContent;
import com.luoqy.speedy.modular.web.model.Fiction;
import com.luoqy.speedy.modular.web.service.IFictionService;
import com.luoqy.speedy.modular.web.service.UpdateFictionService;
import com.luoqy.speedy.modular.web.service.WriteFictionService;
import com.luoqy.speedy.modular.web.utils.FictionUtil;
import com.luoqy.speedy.modular.web.utils.JsoupUtil;

/**
 * 小说抓取实现类
 */
@Service
public class WriteFictionServiceImpl implements WriteFictionService {

    private static final Logger log = LoggerFactory.getLogger(HttpAspect.class);
    
    @Resource
    private IFictionService iFictionService;

    @Resource
    private ChapterContentMapper chapterContentMapper;

    @Resource
    private ChapterMapper chapterMapper;
    @Resource
    private UpdateFictionService updateFictionService;
	/**
	 * 轮询小说列表页面
	 * @param type
	 * @param page
	 * @return
	 */
	@Async
	@Override
	public void poll(String type,int page,WriteFictionService writeFictionService){
		boolean flag=false;
		do {
			String url="http://www.shuquge.com/category/"+type+"_"+page+".html";
			
			Document document = JsoupUtil.getDoc(url);
			if(null!=document){
		        Element element = document.select("div.l").first();
		        Elements elements = element.select("span.s2");
		        if(elements.size()>0){
		        	writeFictionService.insert(url);
					page++;
					flag=true;
					
		        }else{
		        	flag=false;
		        }
			}else{
				flag=false;
			}
		} while (flag);
		
	}
    /**
     * 类型页面小说抓取（多本）
     * @param fictionURL 类型URL
     */
    @Async
    @Override
    public void insert(String fictionURL){
    	//类型小说列表页面
    	Document listPage = JsoupUtil.getDoc(fictionURL);
        
        Element element = listPage.select("div.l").first();
        Elements listFictionHtml = element.select("span.s2");
//        int start=0;
    	for (Element listFictionTag: listFictionHtml) {
   		 	//小说章节链接
            String fictionUrl = listFictionTag.select("a").attr("abs:href");
    		 //章节页面
	    	Document doc = JsoupUtil.getDoc(fictionUrl);
	    	//基础信息
	        Fiction fiction =FictionUtil.getFictions(doc);
	        if (fiction != null) {
	        	fiction.setFictionUrl(fictionUrl);//设置url
	            //查询小说是否存在
	        	QueryWrapper<Fiction> wrapper=new QueryWrapper<Fiction>();
	        	wrapper.eq("fiction_name",fiction.getFictionName());
	        	Fiction ft = iFictionService.getOne(wrapper);
	            if (null==ft) {
	                //小说页面链接
	                //存储小说基础信息
	            	iFictionService.save(fiction);
	                //所有章节
	                Elements elements = doc.select("dd>a");
	                int k = 12;//前面12章节重复
	                int sort = 1;//数据库排序
	                int j = elements.size() - 12;//前面12章节重复
	                //内容处理
	                for (int i = 0; i < j; i++) {
	                	ChapterContent chapterContent=new ChapterContent();
	                	//小说内容链接
	                    String url = elements.get(k).attr("abs:href");
	                    Document document = JsoupUtil.getDoc(url);
	                    if(null!=document){
	                    	//内容页标题
		                    String title = document.select("h1").text();//章节标题
		                    //内容
		                    String text = JsoupUtil.subContent(document.getElementById("content").html());//章节内容
		                     //保存小说内容
		                    chapterContentMapper.insert(chapterContent.setContent(text));
		                    int id = chapterContent.getId();
		                    chapterMapper.insert(new Chapter(url,fiction.getId(), title, sort,id));
		                    //log.info("抓取小说：" + fiction.getFictionName() + "" + title);
		                    sort++;
	                    }
	                    k++;
	                }
	                System.err.println("["+fiction.getFictionName()+"]=抓取完成");
	            } else {
	            	//应更新小说
	            	updateFictionService.updateFiction(ft);
	            	 System.err.println("这本小说已经保存到数据库");
	            }
	           
	        } else {
	            log.info("小说连接地址错误");
	        }
    	}
    }
    /**
     * 抓取小说持久化到mysql数据库=单本
     * @param fictionURL  小说章节页面
     * @return 
     * @return 
     * @throws Exception 
     * 
     */
    @Async
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Future<String> insertResult(String fictionURL){
    		 //爬取小说基本信息
	    	Document doc = JsoupUtil.getDoc(fictionURL);
	    	if(null==doc){
	    		return new AsyncResult<>("error");
	    	}
	        Fiction fiction = FictionUtil.getFictions(doc);
	        if (fiction != null) {
	        	fiction.setFictionUrl(fictionURL);
	            //查询数据库信息
	            QueryWrapper<Fiction> queryWrapper=new QueryWrapper<>();
	            queryWrapper.eq("fiction_name",fiction.getFictionName());
	            Fiction ft = iFictionService.getOne(queryWrapper);
	            if (ft == null) {
	                iFictionService.save(fiction);
	                Elements elements = doc.select("dd>a");
	                int k = 12;//前面12章节重复
	                int sort = 1;//数据库排序
	                int j = elements.size() - 12;//前面12章节重复
	                ChapterContent chapterContent=new ChapterContent();
	                for (int i = 0; i < j; i++) {
	                    String url = elements.get(k).attr("abs:href");
	                    Document document = JsoupUtil.getDoc(url);
	                    if(null!=document){
	                    	 String title = document.select("h1").text();//章节标题
	 	                    String text = JsoupUtil.subContent(document.getElementById("content").html());//章节内容
	 	                     //保存小说内容
	 	                    chapterContentMapper.insert(chapterContent.setContent(text));
	 	                    int id = chapterContent.getId();
	 	                    chapterMapper.insert(new Chapter(url,fiction.getId(), title, sort,id));
	 	                    //log.info("抓取小说：" + fiction.getFictionName() + "" + title);
	 	                    sort++;
	 	                   
	                    }
	                    k++;
	                }
	                log.info("抓取小说完成");
	                return new AsyncResult<>("success");
	            } else {
	            	//应更新小说
	            	updateFictionService.updateFiction(ft);
	                log.info("这本小说已经保存到数据库");
	                return new AsyncResult<>("success");
	            }
	           
	        } else {
	            log.info("小说连接地址错误");
	            return new AsyncResult<>("error");
	        }
    
    }
    /**
     * 基础信息的存储
	 */
    @Async
	@Override
	public void insertBase(String fictionURL) {
    	//类型小说列表页面
    	Document listPage = JsoupUtil.getDoc(fictionURL);
        
        Element element = listPage.select("div.l").first();
        Elements listFictionHtml = element.select("span.s2");
    	for (Element listFictionTag: listFictionHtml) {
   		 	//小说章节链接
            String fictionUrl = listFictionTag.select("a").attr("abs:href");
    		 //章节页面
	    	Document doc = JsoupUtil.getDoc(fictionUrl);
	    	//基础信息
	        Fiction fiction =FictionUtil.getFictions(doc);
	        if (fiction != null) {
	        	fiction.setFictionUrl(fictionUrl);//设置url
	            //查询小说是否存在
	        	QueryWrapper<Fiction> wrapper=new QueryWrapper<Fiction>();
	        	wrapper.eq("fiction_name",fiction.getFictionName());
	        	Fiction ft = iFictionService.getOne(wrapper);
	            if (null==ft) {
	                //存储小说基础信息
	            	iFictionService.save(fiction);
	                System.err.println("["+fiction.getFictionName()+"]=基础信息抓取完成");
	            } else {
	            	//应更新小说
	            	updateFictionService.updateFictionBase(ft);
	            	 System.err.println("["+fiction.getFictionName()+"]这本小说已经保存到数据库");
	            }
	           
	        } else {
	            log.info("小说连接地址错误");
	        }
    	}
    }
}
